<?php

namespace App\Http\Controllers\Admin\Logs;

use App\Http\Controllers\AdminController;
use Illuminate\Http\Request;
use Rap2hpoutre\LaravelLogViewer\LaravelLogViewer;

class ErrorLogController extends AdminController
{

    public function files()
    {
        $logViewer = new LaravelLogViewer;
        $files = $logViewer->getFiles(true);
        $list = [];
        if (!empty($files)) {
            foreach ($files as $key => $row) {
                $list[] = ['label' => $row, 'value' => $key];
            }
        }
        return $this->succeed($list);
    }

    /**
     * @throws \Exception
     */
    public function data(Request $request)
    {
        $logViewer = new LaravelLogViewer;
        if ($fileName = $request->get('fileName')) {
            $logViewer->setFile($fileName);
        }
        $logs = collect($logViewer->all());
        $total = $logs->count();
        $page = (int)$request->get('page', 1);
        $pageSize = (int)$request->get('pageSize', 10);
        $data = $logs->slice(($page-1)*$pageSize, $pageSize)->values();
        return $this->succeed([
            'total' => $total,
            'rows'  => $data
        ]);
    }
}
